package joney.main.views.report;

import java.awt.Color;

import joney.main.analysis.ExpenseUtils;
import joney.main.analysis.ExpenseUtils.DayTotal;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class GraphView extends TimeRangeChart {

	public static String ID = "joney.graphview";


	protected JFreeChart createChart(Dataset dataset) {
		// create the chart...
		JFreeChart chart = ChartFactory.createXYLineChart(
				null, // chart title
				"", // x axis label
				"$", // y axis label
				(XYDataset) dataset, // data
				PlotOrientation.VERTICAL,
				false, // include legend
				false, // tooltips
				false // urls
		);
		
		XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.white);
		
		XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
		renderer.setSeriesShapesVisible(0, true);
		
		return chart;
	}

	protected Dataset createDataset(int yearb, int yeare) {
		XYSeries series1 = new XYSeries("");
		XYSeriesCollection dataset = new XYSeriesCollection();
		dataset.addSeries(series1);
		
		setGraphData(ALL,series1, yearb, yeare);
		
		return dataset;
	}

	private void setGraphData(String accountName, XYSeries series, int yearb, int yeare)
	{
		if(accountName == null){accountName = ALL;}
		
		for(DayTotal dt: ExpenseUtils.getTotalExpenseForDays(accountName, 0, 1, yearb, 0, 11, yeare))
		{
			series.add(dt.day.getTime(), dt.totalValue);
		}
	}
	
	
	protected void updateData(int yearb, int yeare) {
		XYSeriesCollection dds = (XYSeriesCollection)dataset;
		dds.removeAllSeries();
		XYSeries series1 = new XYSeries("");
		dds.addSeries(series1);
		
		setGraphData(ALL,series1, yearb, yeare);
	}

	protected void updateData(String account, int yearb, int yeare) {
		XYSeriesCollection dds = (XYSeriesCollection)dataset;
		dds.removeAllSeries();
		XYSeries series1 = new XYSeries("");
		dds.addSeries(series1);
		
		setGraphData(account,series1, yearb, yeare);
	}}
